2020-2021 Sunseeker Telemetry and Lighting System
adc10_a_ex3_windowComp.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
69 //******************************************************************************
70 #include "driverlib.h"
71 
72 //~2.0V Input
73 #define High_Threshold 818
74 //~1.0V Input
75 #define Low_Threshold 409
76 
77 #define SlowToggle_Period 20000 - 1
78 #define FastToggle_Period 1000 - 1
79 
80 Timer_B_initUpModeParam paramB2 = {0};
81 
82 void main (void)
83 {
84  //Stop Watchdog Timer
85  WDT_A_hold(WDT_A_BASE);
86 
87  paramB2.clockSource = TIMER_B_CLOCKSOURCE_ACLK;
88  paramB2.clockSourceDivider = TIMER_B_CLOCKSOURCE_DIVIDER_1;
89  paramB2.timerPeriod = FastToggle_Period;
90  paramB2.timerInterruptEnable_TBIE = TIMER_B_TBIE_INTERRUPT_DISABLE;
91  paramB2.captureCompareInterruptEnable_CCR0_CCIE =
92  TIMER_B_CCIE_CCR0_INTERRUPT_ENABLE;
93  paramB2.timerClear = TIMER_B_SKIP_CLEAR;
94  paramB2.startTimer = true;
95  Timer_B_initUpMode(TIMER_B0_BASE, &paramB2);
96 
97  //Initialize the ADC10_A Module
98  /*
99  * Base Address for the ADC10_A Module
100  * Use TIMER_A0 to signal a sample/hold-and-convert
101  * USE MODOSC 5MHZ Digital Oscillator as clock source
102  * Use default clock divider of 1
103  */
104  ADC10_A_init(ADC10_A_BASE,
105  ADC10_A_SAMPLEHOLDSOURCE_1,
106  ADC10_A_CLOCKSOURCE_ADC10OSC,
107  ADC10_A_CLOCKDIVIDER_1);
108 
109  ADC10_A_enable(ADC10_A_BASE);
110 
111  /*
112  * Base Address for the ADC10_A Module
113  * Sample/hold for 16 clock cycles
114  * Do not enable Multiple Sampling
115  */
116  ADC10_A_setupSamplingTimer(ADC10_A_BASE,
117  ADC10_A_CYCLEHOLD_16_CYCLES,
118  ADC10_A_MULTIPLESAMPLESDISABLE);
119 
120  //Configure Memory Buffer
121  /*
122  * Base Address for the ADC10_A Module
123  * Use input A1
124  * Use positive reference of Internally generated Vref
125  * Use negative reference of AVss
126  */
127  ADC10_A_configureMemory(ADC10_A_BASE,
128  ADC10_A_INPUT_A1,
129  ADC10_A_VREFPOS_INT,
130  ADC10_A_VREFNEG_AVSS);
131 
132  //Configure Window Comparator
133  /*
134  * Base Address for the ADC10_A Module
135  * Set the high threshold of the window comparator
136  * Set the low threshold of the window comparator
137  */
138  ADC10_A_setWindowComp(ADC10_A_BASE,
140  Low_Threshold);
141 
142  //Enable all window comparator interrupts
143  ADC10_A_clearInterrupt(ADC10_A_BASE,
144  ADC10HIIFG + ADC10LOIFG + ADC10INIFG);
145  ADC10_A_enableInterrupt(ADC10_A_BASE,
146  ADC10HIIE + ADC10LOIE + ADC10INIE);
147 
148  //Configure internal reference
149  //If ref generator busy, WAIT
150  while ( REF_BUSY == Ref_isRefGenBusy(REF_BASE) ) ;
151  //Select internal ref = 1.5V
152  Ref_setReferenceVoltage(REF_BASE,
153  REF_VREF2_5V);
154  //Internal Reference ON
155  Ref_enableReferenceVoltage(REF_BASE);
156 
157  //Delay (~75us) for Ref to settle
158  __delay_cycles(75);
159 
160 
161  //Set P1.0 to output direction
162  GPIO_setAsOutputPin(
163  GPIO_PORT_P1,
164  GPIO_PIN0
165  );
166 
167  //Clear P1.0 LED off
169  GPIO_PORT_P1,
170  GPIO_PIN0
171  );
172 
173  //Configure TB0 period-timer
174  Timer_B_initUpModeParam paramB1 = {0};
175  paramB1.clockSource = TIMER_B_CLOCKSOURCE_ACLK;
176  paramB1.clockSourceDivider = TIMER_B_CLOCKSOURCE_DIVIDER_1;
177  paramB1.timerPeriod = 0;
178  paramB1.timerInterruptEnable_TBIE = TIMER_B_TBIE_INTERRUPT_DISABLE;
179  paramB1.captureCompareInterruptEnable_CCR0_CCIE =
180  TIMER_B_CCIE_CCR0_INTERRUPT_ENABLE;
181  paramB1.timerClear = TIMER_B_DO_CLEAR;
182  paramB1.startTimer = true;
183  Timer_B_initUpMode(TIMER_B0_BASE, &paramB1);
184 
185  //Configure ADC10_A timer trigger TA0.1
186  Timer_A_initCompareModeParam initCompareModeParam = {0};
187  initCompareModeParam.compareRegister = TIMER_A_CAPTURECOMPARE_REGISTER_1;
188  initCompareModeParam.compareInterruptEnable = TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE;
189  initCompareModeParam.compareOutputMode = TIMER_A_OUTPUTMODE_TOGGLE;
190  initCompareModeParam.compareValue = 8192 - 1;
191  Timer_A_initCompareMode(TIMER_A0_BASE, &initCompareModeParam);
192 
193  Timer_A_initUpModeParam paramA = {0};
194  paramA.clockSource = TIMER_A_CLOCKSOURCE_ACLK;
195  paramA.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
196  paramA.timerPeriod = 16384 - 1;
197  paramA.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
198  paramA.captureCompareInterruptEnable_CCR0_CCIE =
199  TIMER_A_CCIE_CCR0_INTERRUPT_DISABLE;
200  paramA.timerClear = TIMER_A_DO_CLEAR;
201  paramA.startTimer = true;
202  Timer_A_initUpMode(TIMER_A0_BASE, &paramA);
203 
204 
205  //Enable and Start the conversion
206  //in Repeated Single-Channel Conversion Mode
207  ADC10_A_startConversion(ADC10_A_BASE,
208  ADC10_A_REPEATED_SINGLECHANNEL);
209 
210  //Enter LPM3 w/ interrupts
211  __bis_SR_register(LPM3_bits + GIE);
212  //for debugger
213  __no_operation();
214 }
215 
216 // ADC10_A interrupt service routine
217 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
218 __interrupt
219 #elif defined(__GNUC__)
220 __attribute__((interrupt(ADC10_VECTOR)))
221 #endif
222 void ADC10_A_ISR (void) {
223 
224  switch (__even_in_range(ADC10IV,12)){
225  case 0: break; //No interrupt
226  case 2: break; //conversion result overflow
227  case 4: break; //conversion time overflow
228  case 6: //ADC10HIIFG
229  //Turn off Timer
230  Timer_B_stop(TIMER_B0_BASE);
231 
232  //Set Timer Period for fast LED toggle
233 
234  paramB2.timerPeriod = FastToggle_Period;
235  Timer_B_initUpMode(TIMER_B0_BASE, &paramB2);
236 
237  break;
238 
239  case 8: //ADC10LOIFG
240  //Turn off Timer
241  Timer_B_stop(TIMER_B0_BASE);
242 
243  //Clear P1.0 LED off
245  GPIO_PORT_P1,
246  GPIO_PIN0
247  );
248  break;
249 
250  case 10: //ADC10INIFG
251  //Turn off Timer
252  Timer_B_stop(TIMER_B0_BASE);
253 
254  //Set Timer Period for slow LED toggle
255  paramB2.timerPeriod = SlowToggle_Period;
256  Timer_B_initUpMode(TIMER_B0_BASE, &paramB2);
257 
258  break;
259  case 12: break; //ADC10IFG0
260  default: break;
261  }
262 }
263 
264 // TIMER_B interrupt service routine
265 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
266 __interrupt
267 #elif defined(__GNUC__)
268 __attribute__((interrupt(TIMER0_B0_VECTOR)))
269 #endif
270 void TIMER0_B0_ISR (void) {
271  // Toggle P1.0
272  GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN0);
273 }
Timer_B_initUpModeParam paramB2
#define SlowToggle_Period
void ADC10_A_ISR(void)
#define FastToggle_Period
void main(void)
void TIMER0_B0_ISR(void)
#define Low_Threshold
#define High_Threshold
__no_operation()
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
__delay_cycles(500000)